package it.marcoberri.mbfasturlexportdata;

import com.google.gson.Gson;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Hello world!
 *
 */
public class Export {

    public static void main(String[] args) {

        try {

            //final String type = args[0];
            //final String type = "0";
            final String type = "1";

            //final String from = args[1];
            //final String from = "/home/marco/mbfasturl/old/mburl.db";
            final String from = "/home/marco/mbfasturl/old/mbstats.db";

            //final String to = args[2];
            //final String to = "/home/marco/mbfasturl/old/mburl.json";
            final String to = "/home/marco/mbfasturl/old/mbstats.json";


            Class.forName("org.sqlite.JDBC");

            Connection conn = DriverManager.getConnection("jdbc:sqlite:" + from);
            Statement statement = conn.createStatement();

            if (type.equals("0")) {

                final ResultSet rs = statement.executeQuery("select * from url");
                final List list = new ArrayList();
                int c = 0;
                while (rs.next()) {

                    final String fast = rs.getString("id");
                    final Date created = rs.getDate("t");
                    final Date ending = rs.getDate("t_end");

                    final Map o = new HashMap();
                    o.put("fast", fast);
                    o.put("created", created);
                    o.put("ending", ending);
                    list.add(o);
                    c++;

                    if (c % 100 == 0) {
                        System.out.println("act:" + c);
                    }
                }

                System.out.println("total:" + c);
                System.out.println("to:" + to);
                rs.close();

                Gson gson = new Gson();

                org.apache.commons.io.FileUtils.writeStringToFile(new File(to), gson.toJson(list).toString(), "utf8");


            } else {


                String[] tables = {"view", "save"};
                final List list = new ArrayList();

                for (String t : tables) {


                    ResultSet rs = statement.executeQuery("select * from " + t);


                    int c = 0;
                    while (rs.next()) {

                        final String fast = rs.getString("fk_url_id");
                        final String header = rs.getString("header");
                        final Date ts = rs.getDate("ts");
                        final String ip = rs.getString("ip");
                        

                        final HashMap headers = new HashMap();
                      
                        if ( header != null && !"".equals(header) ) {
                            String h[] = header.split("\\|\\|");
                            for (String s : h) {
                                String[] v = s.split("\\|", 2);
                                if (v.length < 2) {
                                    continue;
                                }
                               
                                headers.put(v[0].replaceAll("\\.", "_"), v[1]);
                            }
                        }
   
                                             
                        
                        final Map o = new HashMap();
                        o.put("fast", fast);
                        o.put("header", headers);
                        o.put("ts", ts);
                        o.put("ip", ip);
                        o.put("type",t);
                        list.add(o);


                        c++;
                    }
                    System.out.println("total:" + c + " for table:" + t);

                    rs.close();


                }


                System.out.println("to:" + to);
                Gson gson = new Gson();
                org.apache.commons.io.FileUtils.writeStringToFile(new File(to), gson.toJson(list).toString(), "utf8");
            }




        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }


    }
}
